home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / support / dbmmanage.new < prev    next >
Text File  |  1995-12-04  |  5KB  |  134 lines

  1. #!/usr/local/bin/perl
  2.  
  3. # ====================================================================
  4. # Copyright (c) 1995 The Apache Group.  All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. #    notice, this list of conditions and the following disclaimer. 
  12. #
  13. # 2. Redistributions in binary form must reproduce the above copyright
  14. #    notice, this list of conditions and the following disclaimer in
  15. #    the documentation and/or other materials provided with the
  16. #    distribution.
  17. #
  18. # 3. All advertising materials mentioning features or use of this
  19. #    software must display the following acknowledgment:
  20. #    "This product includes software developed by the Apache Group
  21. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  22. #
  23. # 4. The names "Apache Server" and "Apache Group" must not be used to
  24. #    endorse or promote products derived from this software without
  25. #    prior written permission.
  26. #
  27. # 5. Redistributions of any form whatsoever must retain the following
  28. #    acknowledgment:
  29. #    "This product includes software developed by the Apache Group
  30. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  31. #
  32. # THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  33. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35. # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  36. # IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. # OF THE POSSIBILITY OF SUCH DAMAGE.
  44. # ====================================================================
  45. #
  46. # This software consists of voluntary contributions made by many
  47. # individuals on behalf of the Apache Group and was originally based
  48. # on public domain software written at the National Center for
  49. # Supercomputing Applications, University of Illinois, Urbana-Champaign.
  50. # For more information on the Apache Group and the Apache HTTP server
  51. # project, please see <http://www.apache.org/>.
  52.  
  53.  
  54. # usage: dbmmanage <DBMfile> <command> <key> <value>
  55. #
  56. # commands: add, delete, view, adduser
  57. #
  58. # no values needed for delete, no keys or values needed for view.
  59. # to change a value, simply use "add".
  60. # adduser encrypts the password:
  61. # dbmmanage <dbm file> adduser <person> <password>
  62.  
  63. die "Too few arguments.\n" if (@ARGV < 2);
  64.  
  65. ($file,$command,$key,$value) = @ARGV;
  66.  
  67. $file =~ s/\.db.?$//;        # remove ".db" or ".dbX" extension if any
  68. $file =~ s/\.pag$//;        # remove ".pag" and ".dir" as well.
  69. $file =~ s/\.dir$//;        # these are all common DBM extensions.
  70.  
  71. if ($command eq "add") {
  72.   dbmopen(%DB, $file, 0664) || die "Error: $!\n";
  73.   $DB{$key} = $value;
  74.   dbmclose(%DB);
  75.   print "Entry $key added with value $value.\n";
  76. } elsif ($command eq "adduser") {
  77.   srand;            # needs to be done only once.
  78.   $salt = &compute_salt(0);    # change to compute_salt(1) for new crypt()
  79.   $hash = crypt($value, $salt);
  80.   dbmopen(%DB, $file, 0664) || die "Error: $!\n";
  81.   $DB{$key} = $hash;
  82.   dbmclose(%DB);
  83.   print "User $key added with password ``$value'', encrypted to $hash\n";
  84. } elsif ($command eq "delete") {
  85.   dbmopen(%DB, $file, 0664) || die "Error: $!\n";
  86.   delete($DB{$key});
  87.   dbmclose(%DB);
  88. } elsif ($command eq "view") {
  89.   dbmopen(%DB, $file, undef) || die "Error: $!\n";
  90.   unless ($key) {
  91.     while (($nkey,$val) = each %DB) {
  92.       print "$nkey = $val\n";
  93.     }
  94.   } else {
  95.     print "$key = $DB{$key}\n";
  96.   } 
  97.   dbmclose(%DB);
  98. } else {
  99.   print "Command unrecognized - must be one of: view, add, adduser, delete.\n";
  100. }
  101.  
  102. exit(0);
  103.  
  104. # if $newstyle is 1, then use new style salt (starts with '_' and contains
  105. # four bytes of iteration count and four bytes of salt).  Otherwise, just use
  106. # the traditional two-byte salt.
  107. # see the man page on your system to decide if you have a newer crypt() lib.
  108. # I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
  109. # The new style crypt() allows up to 20 characters of the password to be
  110. # significant rather than only 8.
  111. sub compute_salt {
  112.   local($newstyle) = @_;
  113.   local($salt);
  114.   if ($newstyle) {
  115.     $salt = "_" . &randchar(1) . "a.." . &randchar(4);
  116.   } else {
  117.     $salt = &randchar(2);
  118.   }
  119.   $salt;
  120. }
  121.  
  122. # return $count random characters
  123. sub randchar {
  124.   local($count) = @_;
  125.   local($str) = "";
  126.   local($enc) =
  127.     "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  128.   while ($count--) {
  129.     # 64 = length($enc) in call to rand() below
  130.     $str .= substr($enc,int(rand(64))+1,1);
  131.   }
  132.   $str;
  133. }
  134.